package com.javatest.springboot2kafka.kafka;

import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @date 2021-03-02 下午4:33
 */
@Component
public class KafkaProducer {
    private static final Logger LOGGER = LoggerFactory.getLogger(KafkaProducer.class);
    private static final String MY_TOPIC = "my-test";

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    @Scheduled(fixedDelay = 2_000)
    public void send() {
        kafkaTemplate.send(MY_TOPIC, "key-" + ThreadLocalRandom.current().nextInt(), UUID.randomUUID().toString());
        LOGGER.info("Send kafka message to topic-{}", MY_TOPIC);
    }
}
